<template>
<div class="tc-left pr10 fl">
    <ul class="clearfix uiTab1 mb5 j_outerHeight">
        <!-- <router-link tag="li" class="col-md-4 fl" to="/projectFiles/scene"><a href="javascript:">项</a></router-link>
        <router-link tag="li" class="col-md-4 fl uiTab1-active" to="/projectFiles/SceneLibrary"><a href="javascript:">现场库</a></router-link> -->
    </ul>
    <div class="daily_title j_outerHeight">
        <ul class="clearfix">
            <router-link tag="li"  to="/projectFiles/ServerProject" active-class="title_cur" data-title="全部">全</router-link>
            <span aria-hidden="true" class="iconfont icon-add icon-2x bold pointer cBlue fr mt2 mr5" @click="addPage()" data-title="添加"></span>
        </ul>
    </div>
    <div class="contentbox">
        <div class="dailyftSearch clearfix" v-if="souFlag" style="z-index:2">
            <div class="fl uiHas-textIcon">
                <input type="text" placeholder="编号/项目单号/项经/监理" class="col-md-12 searchName" v-model="searchText"  @keyup.enter="selectFilter()">
                <i class="uiText-searchIcon bgIcon" @click="selectFilter()"></i>
            </div>
        </div>
        <table class="uiTable j_outerHeight">
            <thead>
                <tr>
                    <th width="20%">
                       <span class="serial_number" @click="souFlag = true">编号<i class="bgIcon"></i></span>
                    </th>
                    <th width="20%" class="tx-center">科目</th>
                    <th width="33%" class="tx-center">内容</th>
                    <th width="12%" class="tx-center">
                         <select class="theadselect thinScroll" v-model="statusValue" @change="selectChange()">
                            <option value="">状态</option>
                            <option value="0">未</option>
                            <option value="1">完</option>
                        </select>
                    </th>
                    <th width="15%">
                        <select class="theadselect thinScroll" v-model="openValue" @change="openChange()">
                            <option value="">开关</option>
                            <option value="0">开启</option>
                            <option value="1">关闭</option>
                        </select>
                    </th>
                </tr>
            </thead>
        </table>
        <div class="scroll-content thinScroll thinScroll-table" v-loading="loading" v-scrollHeight="36">
            <table class="uiTable uiTable-striped uiTable-hover" id="tab1">
                <tbody>
                    <tr v-for="(item,index) in serverList" :key="index" :class="{'tractive':index == trIndex}" @click="siderBarTrclick(index,item)">
                        <td width="20%" class="relative">{{item.id}}</td>
                        <td width="20%" :data-title='item.subjectName' >{{item.subjectName | truncate(3)}}</td>
                        <td width="33%" :data-title='item.content'>{{item.content | truncate(5)}}</td>
                        <td width="12%">{{item.status === 1 ? '完':'未'}}</td>
                        <td width="15%">{{item.delFlag === 1 ?'关闭':'开启'}}</td>
                    </tr>
                    <tr v-if="serverList.length === 0">
                        <td>暂无数据</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="j_outerHeight clearfix tj_bottom">
            <p class="col-md-3" data-title="全部">{{allCount}}</p>
            <p class="col-md-3 cGreen" data-title="正常">{{zcCount}}</p>
            <p class="col-md-3 cOrange" data-title="异常">{{ycCount}}</p>
            <p class="col-md-3 cRed" data-title="问题">{{wtCount}}</p>
        </div>
    </div>
</div>
</template>
<script>
import { mapMutations, mapGetters } from 'vuex'
import { getItem } from '../Resources/Api/index'
export default {
    data () {
        return {
            loading: false, // 加载东湖a
            trIndex: 0, // 当前的Index
            serverList: [], // 服务项集合
            serverListCloneList: [], // 服务项集合复制 用于筛选
            serverCopyList: [], // 用于存全部数据
            statusValue: '', // 状态Value
            openValue: '', // 开启关闭Value
            searchText: '', // 模糊查询
            souFlag: false, // 搜索框出现
            allCount: 0, // 全部
            zcCount: 0, // 正常
            ycCount: 0, // 异常
            wtCount: 0 // 问题
        }
    },
    computed: {
        ...mapGetters(['leftInfo', 'updatePlate'])
    },
    created () {
        this.load()
    },
    methods: {
        ...mapMutations({
            setLeftData: `SET_LEFT_INFO`
        }),
        // 点击 + 号二段弹窗页面
        addPage () {
            this.$router.push('/projectFiles/ServerProject/addPage')
        },
        siderBarTrclick (index, item) {
            this.trIndex = index
            // 保存字段时 一定要确认公共字段一致
            this.setLeftData(item)
            // 关闭弹窗
            this.$router.push(this.$route.matched[1].path)
        },
        // 筛选
        selectFilter () {
            this.trIndex = 0
            this.souFlag = false
            let stageReg = this.searchText.replace(/\s+/g, '')
            if (stageReg === '') {
                this.serverList = this.serverCopyList
            } else {
                const sereverData = this.serverListCloneList.filter(item => {
                    return (
                        item.subjectName.match(stageReg) || item.content.match(stageReg)
                    )
                })
                if (sereverData && sereverData.length >= 0) {
                    this.serverList = sereverData
                    if (sereverData && sereverData.length > 0) {
                        this.serverListCloneList = this.serverList
                    }
                    this.statusValue = '' // 状态Value
                    this.openValue = '' // 开启关闭Value
                }
            }
            this.updateBottom()
        },
        // 开启 关闭
        openChange () {
            if (this.openValue === '') {
                this.serverList = this.serverListCloneList
            } else {
                const sereverData = this.serverListCloneList.filter(item => { return Number(item.delFlag) === Number(this.openValue) })
                if (sereverData && sereverData.length >= 0) {
                    this.serverList = sereverData
                }
            }
            this.updateBottom()
        },
        // 完 未
        selectChange () {
            if (this.statusValue === '') {
                this.serverList = this.serverListCloneList
            } else {
                const sereverData = this.serverListCloneList.filter(item => { return Number(item.status) === Number(this.statusValue) })
                if (sereverData && sereverData.length >= 0) {
                    this.serverList = sereverData
                }
            }
            this.updateBottom()
        },
        // 加载数据
        load () {
            this.trIndex = 0
            // 用_this 代替this
            let _this = this
            _this.loading = true
            let params = {}
            // 接口获取数据，对项目阶段进行转换
            getItem(params).then(results => {
                if (results.data.StatusCode === 1) {
                    _this.serverList = results.data.Body.itemList
                    this.serverListCloneList = _this.serverList
                    this.serverCopyList = this.serverList
                    this.siderBarTrclick(this.trIndex, _this.serverList[this.trIndex])
                    this.updateBottom()
                }
            }).catch(error => {
                console.log(error)
            })
            _this.loading = false
        },
        // 更新底部正常异常问题的数量
        updateBottom () {
            let zcCount = 0
            let ycCount = 0
            let wtCount = 0
            this.allCount = this.serverList.length
            this.zcCount = zcCount
            this.ycCount = ycCount
            this.wtCount = wtCount
        }
    },
    watch: {
        updatePlate () {
            this.load()
        }
    }
}
</script>
